package org.raymond.iworks.study.redis.redisson;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@Slf4j
public class CountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {
        test();
    }

    public static void test() throws InterruptedException {
        RedissonClient client = client();
        RCountDownLatch rcdl = client.getCountDownLatch("myCountDownLatch");
        rcdl.trySetCount(2);
        for(int i=0; i<2; i++) {
            final int j = i;
            new Thread(() -> {
                log.info("do something");
                try {
                    Thread.sleep(j*1000);
                } catch (InterruptedException e) {
                }
                log.info("count down ready");
                rcdl.countDown();
                log.info("count down done");
            }).start();
        }

        log.info("await...");
        rcdl.await();
        log.info("await pass");

        rcdl.delete();
        client.shutdown();
    }

    public static RedissonClient client(){
        Config cfg = new Config();
        cfg.useSingleServer().setAddress("redis://192.168.0.125:6379");
        RedissonClient client = Redisson.create(cfg);
        return client;
    }
}
